﻿namespace Pub.Class
{
    using System;
    using System.Collections.Generic;
    using System.Web;

    public class Cookie2
    {
        public static void Clear()
        {
            IList<string> list = new List<string>();
            foreach (string str in HttpContext.Current.Request.Cookies)
            {
                list.Add(str);
            }
            foreach (string str2 in list)
            {
                HttpCookie cookie = new HttpCookie(str2) {
                    Expires = DateTime.Now.AddDays(-1.0)
                };
                HttpContext.Current.Response.AppendCookie(cookie);
            }
        }

        public static string Get(string key)
        {
            return Get("Pub.Class.Cookies", key);
        }

        public static string Get(string name, string key)
        {
            string decryptKey = "9cf8d21d394a8919d2f9706dfdc6421e";
            string str2 = (decryptKey + name).MD5();
            string str3 = (decryptKey + key).MD5();
            string str = string.Empty;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[str2];
            if (cookie.IsNotNull() && !string.IsNullOrEmpty(cookie.Values[str3]))
            {
                str = cookie.Values[str3].ToString().AESDecode(decryptKey);
            }
            if (!str.IsNullEmpty())
            {
                return str;
            }
            return string.Empty;
        }

        public static void Set(string key, string value)
        {
            Set("Pub.Class.Cookies", key, value);
        }

        public static void Set(string key, string value, DateTime expires)
        {
            Set("Pub.Class.Cookies", key, value, expires, null);
        }

        public static void Set(string key, string value, int days)
        {
            Set("Pub.Class.Cookies", key, value, (days > 0) ? DateTime.Now.AddDays((double) days) : DateTime.MinValue);
        }

        public static void Set(string name, string key, string value)
        {
            Set(name, key, value, DateTime.MinValue);
        }

        public static void Set(string key, string value, DateTime expires, string domain)
        {
            Set("Pub.Class.Cookies", key, value, expires, domain);
        }

        public static void Set(string name, string key, string value, DateTime expires)
        {
            Set(name, key, value, expires, null);
        }

        public static void Set(string name, string key, string value, int days)
        {
            Set(name, key, value, (days > 0) ? DateTime.Now.AddDays((double) days) : DateTime.MinValue);
        }

        public static void Set(string name, string key, string value, string domain)
        {
            Set(name, key, value, DateTime.MinValue, domain);
        }

        public static void Set(string name, string key, string value, DateTime expires, string domain)
        {
            Set(name, key, value, expires, domain, null);
        }

        public static void Set(string name, string key, string value, DateTime expires, string domain, string path)
        {
            Set(name, key, value, expires, domain, path, false);
        }

        public static void Set(string name, string key, string value, DateTime expires, string domain, string path, bool httpOnly)
        {
            Set(name, key, value, expires, domain, path, httpOnly, false);
        }

        public static void Set(string name, string key, string value, DateTime expires, string domain, string path, bool httpOnly, bool secure)
        {
            string encryptKey = "9cf8d21d394a8919d2f9706dfdc6421e";
            string str2 = (encryptKey + name).MD5();
            string str3 = (encryptKey + key).MD5();
            string str4 = value.AESEncode(encryptKey);
            HttpCookie cookie = HttpContext.Current.Request.Cookies[str2];
            if (cookie.IsNull())
            {
                cookie = new HttpCookie(str2);
            }
            cookie.Values[str3] = str4;
            if (expires > DateTime.MinValue)
            {
                cookie.Expires = expires;
            }
            if (!string.IsNullOrEmpty(domain))
            {
                cookie.Domain = domain;
            }
            if (!string.IsNullOrEmpty(path))
            {
                cookie.Path = path;
            }
            cookie.HttpOnly = httpOnly;
            cookie.Secure = secure;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        public static void SetP3PHeader()
        {
            HttpContext.Current.Response.AddHeader("P3P", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
        }
    }
}

